let loopTimer;

let timer;

let onDrawing = false;

// 是否可进行抽奖标识，默认为false可进行抽奖
let drawIndex = 0;

//抽奖过程KEY
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        // 奖品参数信息
        prizeList: [],
        drawIndex: null,
        //抽奖过程KEY
        prizeResult: null,
        //抽奖结果KEY
        prizeName: null,
        //抽奖结果KEY对应的奖品名称
        isShowLuck: false,
        // 是否显示奖品弹窗，默认false不显示
        showAgain: false,
        //是否抽奖后显示再抽一次按钮
        awardImage: "",
        // 弹窗展示的奖品照片
        awardName: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        that.getBase();
        //直播间列表
        },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //抽奖基础信息
    getBase: function() {
        let that = this;
        common.request({
            endpoint: "Roulette/roulette/luck-info",
            method: "post",
            data: {
                act_id: "46"
            },
            success: res => {
                console.log(res);
                that.setData({
                    act_name: res.extra.act_name,
                    act_date: res.extra.act_date,
                    act_backColor: res.extra.act_backColor,
                    act_rule: res.extra.act_rule,
                    my_luckDraw_record: res.extra.my_luckDraw_record,
                    integral: res.extra.integral,
                    count_luck: res.extra.count_luck,
                    prizeList: res.extra.prize
                });
            }
        });
    },
    // 关闭奖品弹簧
    closePopup() {
        this.setData({
            isShowLuck: false
        });
    },
    //点击再抽一次按钮
    againBtn() {
        onDrawing = false;
        drawIndex = 0;
        //抽奖过程KEY
                this.setData({
            drawIndex: null,
            // 清空抽奖过程KEY
            prizeResult: null,
            // 清空抽奖结果KEY
            prizeName: null,
            // 清空抽奖结果KEY对应的奖品名称
            showAgain: false,
            // 是否抽奖后显示再抽一次按钮
            awardImage: "",
            // 清空奖品展示的图片
            awardName: ""
        });
    },
    //抽奖过程奖品切换
    changePrize() {
        drawIndex++;
        drawIndex = drawIndex > 8 ? 1 : drawIndex;
        this.setData({
            drawIndex: drawIndex
        });
        if (this.data.prizeResult == drawIndex) {
            clearInterval(loopTimer);
            clearTimeout(timer);
            let currentAward = this.data.prizeList[this.data.prizeResult - 1];
            this.setData({
                showAgain: true,
                isShowLuck: true,
                awardImage: currentAward.prize_img,
                awardName: currentAward.prize_name
            });
        }
    },
    //抽奖操作
    startDraw() {
        if (onDrawing) {
            return;
        } else {
            onDrawing = true;
        }
        clearInterval(loopTimer);
        clearTimeout(timer);
        loopTimer = setInterval(() => {
            this.changePrize();
        }, 100);
        // 随机将品池
                let random = Math.floor(Math.random() * this.data.prizeList.length);
        let res = {
            stutus: 1,
            prizeResult: this.data.prizeList[random].index,
            prizeName: this.data.prizeList[random].prizeName
        };
        if (res.stutus == 1) {
            timer = setTimeout(() => {
                clearInterval(loopTimer);
                loopTimer = setInterval(() => {
                    this.changePrize();
                }, 200);
                timer = setTimeout(() => {
                    clearInterval(loopTimer);
                    loopTimer = setInterval(() => {
                        this.changePrize();
                    }, 280);
                    timer = setTimeout(() => {
                        clearInterval(loopTimer);
                        loopTimer = setInterval(() => {
                            this.changePrize();
                        }, 380);
                        timer = setTimeout(() => {
                            this.setData({
                                prizeResult: res.prizeResult,
                                prizeName: res.prizeName
                            });
                        }, 1300);
                    }, 1400);
                }, 1500);
            }, 1600);
        }
    }
});